package com.ruoyi.safety.service.impl;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.safety.domain.Area;
import com.ruoyi.safety.dto.AreaDto;
import com.ruoyi.safety.mapper.AreaMapper;
import com.ruoyi.safety.service.IAreaService;
import com.ruoyi.safety.vo.AreaVo;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 区域Service业务层处理
 * 
 * @author lth
 * @date 2024-03-12
 */
@Service
public class AreaServiceImpl implements IAreaService 
{
    @Autowired
    private AreaMapper areaMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询区域
     * 
     * @param areaId 区域主键
     * @return 区域
     */
    @Override
    public Area selectAreaByAreaId(String areaId)
    {
        return areaMapper.selectAreaByAreaId(areaId);
    }

    /**
     * 查询区域列表
     * 
     * @param area 区域
     * @return 区域
     */
    @Override
    public List<Area> selectAreaList(Area area)
    {
        return areaMapper.selectAreaList(area);
    }

    /**
     * 新增区域
     * 
     * @param area 区域
     * @return 结果
     */
    @Override
    public int insertArea(Area area)
    {
//        //获取用户名
//        long userId = Long.parseLong(area.getResponsiblePersonId());
//        area.setResponsiblePersonName(sysUserMapper.selectUserById(userId).getNickName());
//        //获取部门名
//        long deptId = Long.parseLong(area.getResponsibleDepartmentId());
//        area.setResponsibleDepartmentName(sysDeptMapper.selectDeptById(deptId).getDeptName());
        return areaMapper.insertArea(area);
    }

    /**
     * 修改区域
     * 
     * @param area 区域
     * @return 结果
     */
    @Override
    public int updateArea(Area area)
    {
        return areaMapper.updateArea(area);
    }

    /**
     * 批量删除区域
     * 
     * @param areaIds 需要删除的区域主键
     * @return 结果
     */
    @Override
    public int deleteAreaByAreaIds(String[] areaIds)
    {
        return areaMapper.deleteAreaByAreaIds(areaIds);
    }

    /**
     * 删除区域信息
     * 
     * @param areaId 区域主键
     * @return 结果
     */
    @Override
    public int deleteAreaByAreaId(String areaId)
    {
        return areaMapper.deleteAreaByAreaId(areaId);
    }

    /**
     * 通过mineId查询区域信息
     * @param mineId
     * @return
     */
    @Override
    public List listAreaByMineId(String mineId) {
        return areaMapper.listAreaByMineId(mineId);
    }

    /**
     * 通过areaId查询区域信息
     * @param areaId
     * @return
     */
    @Override
    public AreaDto AreaById(String areaId) {
        AreaDto areaDto = areaMapper.AreaById(areaId);
        if (areaDto.getResponsibleDepartmentId() != null && !areaDto.getResponsibleDepartmentId().equals("")){
            SysDept sysDept = sysDeptMapper.selectDeptById(Long.parseLong(areaDto.getResponsibleDepartmentId()));
            if (sysDept != null) {
                areaDto.setResponsibleDepartmentName(sysDept.getDeptName());
            }
        }
        if (areaDto.getResponsiblePersonId() != null && !areaDto.getResponsiblePersonId().equals("")){
            SysUser user = sysUserMapper.selectUserById(Long.parseLong(areaDto.getResponsiblePersonId()));
            if (user != null) {
                areaDto.setResponsiblePersonName(user.getNickName());
            }
        }

        return areaDto;
    }

    /**
     * 获取区域信息通过煤矿ID
     * @param mineId
     * @return
     */
    @Override
    public List<Area> getAreaByMineId(String mineId) {
        return areaMapper.getAreaByMineId(mineId);
    }

    /**
     * 获取区域通过名称
     * @param areaName
     * @return
     */
    @Override
    public Area getAreaByName(String areaName) {
        return areaMapper.getAreaByName(areaName);
    }

    /**
     * 查询区域列表
     * @param areaVo
     * @return
     */
    @Override
    public List<AreaDto> getAreaList(AreaVo areaVo) {
        List<AreaDto> areaDtoList = areaMapper.getAreaList(areaVo);
        List<AreaDto> collect = areaDtoList.stream().map(areaDto -> {
            AreaDto areaDto1 = new AreaDto();
            BeanUtils.copyProperties(areaDto, areaDto1);
            SysDept sysDept = sysDeptMapper.selectDeptById(Long.parseLong(areaDto.getResponsibleDepartmentId()));
            if (sysDept != null) {
                areaDto1.setResponsibleDepartmentName(sysDept.getDeptName());
            }
            SysUser user = sysUserMapper.selectUserById(Long.parseLong(areaDto.getResponsiblePersonId()));
            if (user != null) {
                areaDto1.setResponsiblePersonName(user.getNickName());
            }
            return areaDto1;
        }).collect(Collectors.toList());
        return collect;
    }
//        return areaMapper.getAreaList(areaVo);
}
